/**
 * 
 */
package com.smarthome.core.area.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.common.tools.BossCachedKeysUtil;
import com.smarthome.core.area.AreaProvinceService;
import com.smarthome.core.area.BossAreaService;
import com.smarthome.domain.bean.ShAreaCity;
import com.smarthome.domain.bean.ShAreaDistrict;
import com.smarthome.domain.bean.ShAreaProvince;

/**
 * @author dhc
 * @date 2015年10月13日 下午5:13:49
 * @title 获取区域Util类
 */
@Service("bossAreaService")
public class BossAreaServiceImpl implements BossAreaService {

	private  Map<String,Object> areaMap = new HashMap<String, Object>();
	
    @Resource(name = "areaProvinceService")
    private AreaProvinceService areaProvinceService;

    // @PostConstruct
    private  Map  getMap() {
    	if(!areaMap.isEmpty()){
    		return areaMap ;
    	}
        List<ShAreaProvince> plist = areaProvinceService.queryShAreaProvinces(null);
        areaProvinceService.addParam(plist);
        areaMap.put(BossCachedKeysUtil.AREA_PROVINCE, plist);

        if (null != plist) {
            List<ShAreaCity> allCity = new ArrayList<ShAreaCity>();
            List<ShAreaDistrict> allDistricts = new ArrayList<ShAreaDistrict>();
            for (ShAreaProvince p : plist) {
                if (null == p)
                    continue;
                areaMap.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_PROVINCE, p.getId().toString()), p);
                List<ShAreaCity> clist = p.getCitys();
                if (null == clist)
                    continue;
                allCity.addAll(clist);
                for (ShAreaCity city : clist) {
                    if (null == city)
                        continue;
                    areaMap.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_CITY, city.getId().toString()), city);
                    List<ShAreaDistrict> districts = city.getDistricts();
                    if (null == districts)
                        continue;
                    allDistricts.addAll(districts);
                    for (ShAreaDistrict d : districts) {
                    	areaMap.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_DISTRICTS, d.getId().toString()), d);
                    }
                }
            }
            areaMap.put(BossCachedKeysUtil.AREA_CITY, allCity);
            areaMap.put(BossCachedKeysUtil.AREA_DISTRICTS, allDistricts);
        }

    	return areaMap ;
    }

    /**
     * @title 获取所有的区县
     * @createTime 2015年10月13日 下午5:16:43
     * @author dhc
     */
    public List<ShAreaProvince> getAllProvince() {
        List list = (List) getMap() .get(BossCachedKeysUtil.AREA_PROVINCE);
        return list;
    }

    /**
     * @title 根据id，获取
     * @createTime 2015年10月13日 下午5:55:18
     * @author dhc
     */
    public ShAreaProvince getProvince(Long id) {
        return (ShAreaProvince) getMap() .get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_PROVINCE, id.toString()));
    }

    /**
     * @title 获取所有的城市列表
     * @createTime 2015年10月13日 下午5:57:01
     * @author dhc
     */
    public List<ShAreaCity> getAllCity() {
        return (List) getMap() .get(BossCachedKeysUtil.AREA_CITY);
    }

    /**
     * @title 根据id，获取城市列表
     * @createTime 2015年10月13日 下午5:59:38
     * @author dhc
     */
    public ShAreaCity getCity(Long id) {
        return (ShAreaCity) getMap() .get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_CITY, id.toString()));
    }

    /**
     * @title
     * @createTime 2015年10月13日 下午6:02:34
     * @author dhc
     */
    public List<ShAreaDistrict> getAllDistrict() {
        return (List) getMap() .get(BossCachedKeysUtil.AREA_DISTRICTS);
    }

    /**
     * @title
     * @createTime 2015年10月13日 下午6:02:30
     * @author dhc
     */
    public ShAreaDistrict getDistrict(Long id) {
        return (ShAreaDistrict) getMap() .get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_DISTRICTS, id.toString()));
    }

    /**
     * @title 刷新缓存
     * @createTime 2015年10月13日 下午5:26:35
     * @author dhc
     */
    public boolean reflashAll() {
        boolean isTrue = false;
        try {
        	getMap();
            isTrue = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }
}
